<?php

namespace Kangcg\Application\Base\Trait;

trait ErrorsTrait
{
    protected array $_errors = [];

    /**
     * 设置错误信息
     * @param string | int $code 错误码
     * @param string $message 错误信息
     * @return bool
     */
    public function setErrors($code, $message): bool
    {
        $this->_errors[] = [
            'code' => $code,
            'message' => $message,
        ];

        return false;
    }

    /**
     * @param string $errorMsg 错误信息
     * @param string | int $code 错误码
     * @return bool
     */
    public function setErrorMessage($errorMsg, $code = -200): bool
    {
        return $this->setErrors($code, $errorMsg);
    }

    /**
     * 获取错误码和错误信息
     * @return array|false ['code' => '', 'message' => '']
     */
    public function getError(): array|bool
    {
        return isset($this->_errors[0]) ? $this->_errors[0] : false;
    }

    public function getErrorCode()
    {
        return isset($this->_errors[0]['code']) ? $this->_errors[0]['code'] : null;
    }

    public function getErrorMessage(): string
    {
        return isset($this->_errors[0]['message']) ? $this->_errors[0]['message'] : '';
    }

    public function getErrors(): array
    {
        return $this->_errors;
    }
}
